package workspace;

/**
 * @Description 携程笔试
 * @Author yangshunjie
 * @Date 2021/11/9
 */
public class test10 {


    private static final int MAX_NODE_COUNT = 100;

    public static void printList(ListNode head) {
        int printed = 0;
        while (head != null && printed++ < MAX_NODE_COUNT) {
            System.out.print(head.val + " -> ");
            head = head.next;
        }
        if (printed > MAX_NODE_COUNT) {
            System.out.print("...\n");
            System.err.print("ERROR: too much list nodes, maybe infinite loop...\n");
        } else {
            System.out.print(" null\n");
        }
    }

    public static ListNode makeList(int... values) {
        ListNode node = new ListNode(-1), current = node;
        for (int value : values) {
            current.next = new ListNode(value);
            current = current.next;
        }
        return node.next;
    }

    public static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    /**
     * @param l1 2 -> 4 -> 6
     * @param l2 1 -> 3 -> 5
     * @return 3 -> 7 -> 1 -> 1
     */
    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int carry = 0;
        ListNode dumy = new ListNode(-1);
        ListNode cur = dumy;
        while(l1!=null||l2!=null){
            int a = l1==null?0:l1.val;
            int b = l2==null?0: l2.val;
            int num = a+b+carry;
            carry = num/10;
            cur.next = new ListNode(num%10);
            cur = cur.next;

            if(l1!=null){
                l1 = l1.next;
            }
            if(l2!=null){
                l2 = l2.next;
            }
        }
        if(carry!=0){
            cur.next = new ListNode(carry);
        }
        return dumy.next;

    }

    /**
     * @param str "1" "-10"
     * @return 1 -10
     */
    public static int atoi(String str) {
        return 0;
    }

    public static void main(String[] args) {
        printList(addTwoNumbers(makeList(2, 4, 6), makeList(3, 5, 7,9)));
    }

}
